package com.youku.plugins.explugin.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.events.ConductorEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.plugins.explugin.ExPluginEvent;
   import com.youku.events.ExPluginControlEvent;
   import com.youku.data.PlayerConstant;
   import com.youku.events.PluginEvent;
   import com.youku.interfaces.ICaptionData;
   import com.youku.interfaces.IThreeDData;
   import flash.events.Event;
   import com.youku.utils.ReportUtil;
   import com.youku.conductor.ConductorState;
   
   public class ExPluginMediator extends Object
   {
      
      public function ExPluginMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this.THREED_SWF_LOCATION = param2.rootGetData.rootBaseURL + "/threed.swf";
         this.CAPTION_SWF_LOCATION = param2.rootGetData.rootBaseURL + "/caption.swf";
         this.init();
      }
      
      public static const NAME:String = "ExPluginMediator";
      
      private var THREED_SWF_LOCATION:String = "./threed.swf";
      
      private var CAPTION_SWF_LOCATION:String = "./caption.swf";
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _threedExplugin:ExPluginItem;
      
      private var _captionExplugin:ExPluginItem;
      
      private var _isRending:Boolean;
      
      private var _isVideoPlaying:Boolean;
      
      private var _isHidedToolbox:Boolean = true;
      
      private var _isTVMode:Boolean;
      
      private function init() : void {
         this._threedExplugin = new ExPluginItem(this._mainMc);
         this._captionExplugin = new ExPluginItem(this._mainMc);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetaDataUpdated);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_ERROR,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.BEFORE_SHOW_PANELS,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_EX_PLUGIN,this.onPlayerControlEvent);
         this._threedExplugin.addEventListener(ExPluginEvent.ITEM_LOAD_COMPLETE,this.onThreedLoadComplete);
         this._threedExplugin.addEventListener(ExPluginEvent.ITEM_LOAD_ERROR,this.onThreedLoadError);
         this._captionExplugin.addEventListener(ExPluginEvent.ITEM_LOAD_COMPLETE,this.onCaptionLoadComplete);
         this._captionExplugin.addEventListener(ExPluginEvent.ITEM_LOAD_ERROR,this.onCaptionLoadError);
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:String = null;
         switch(param1.type)
         {
            case PlayerControlEvent.BEFORE_SHOW_PANELS:
               _loc2_ = param1.data.panel;
               this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_HIDE_ALL,{"exclude":_loc2_}));
               break;
            case PlayerControlEvent.SHOW_EX_PLUGIN:
               _loc2_ = param1.data.panel;
               if(_loc2_ == PlayerConstant.PANEL_THREED)
               {
                  this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_SHOW_3D));
               }
               else if(_loc2_ == PlayerConstant.PANEL_CAPTION)
               {
                  this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_SHOW_CC));
               }
               
               break;
            case PlayerControlEvent.COMMON_ERROR:
            case PlayerControlEvent.COMMON_RESET:
               this._threedExplugin.close();
               this._captionExplugin.close();
               this._isRending = false;
               break;
         }
      }
      
      private function onCaptionLoadError(param1:ExPluginEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_REMOVE_DEPENDECE,this._captionExplugin));
      }
      
      private function onCaptionLoadComplete(param1:ExPluginEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_ADD_BUTTON_CAPTION));
         this._captionExplugin.addEventListener("report",this.onCaptionEvent);
         this._captionExplugin.addEventListener("hide",this.onCaptionEvent);
         this._captionExplugin.addEventListener("state_change",this.onCaptionEvent);
         var _loc2_:ICaptionData = this._playerProxy.playerData.captionInfo;
         this._captionExplugin.init(_loc2_);
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_REMOVE_DEPENDECE,this._captionExplugin));
      }
      
      private function onCaptionEvent(param1:ExPluginEvent) : void {
         switch(param1.type)
         {
            case "report":
               this.sendActionLog(param1);
               break;
            case "hide":
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_CAPTION_HIDE));
               break;
            case "state_change":
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
                  {
                     "type":"threeD",
                     "state":param1.data.state
                  }));
               break;
         }
      }
      
      private function onThreedLoadError(param1:ExPluginEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_REMOVE_DEPENDECE,this._threedExplugin));
      }
      
      private function onThreedLoadComplete(param1:ExPluginEvent) : void {
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_ADD_BUTTON_THREED));
         this._threedExplugin.addEventListener("threed_render",this.onThreeDEvent);
         this._threedExplugin.addEventListener("report",this.onThreeDEvent);
         this._threedExplugin.addEventListener("hide",this.onThreeDEvent);
         this._threedExplugin.addEventListener("state_change",this.onThreeDEvent);
         this._threedExplugin.addEventListener("threed_mode_tv",this.onThreeDEvent);
         this._threedExplugin.addEventListener("threed_mode_other",this.onThreeDEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_TOOLBOX,this.onToolboxEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_EX_PLUGIN,this.onToolboxEvent);
         this._playerProxy.addEventListener(PluginEvent.TOOLBOX_CLOSE,this.onToolboxEvent);
         this._playerProxy.addEventListener(PluginEvent.EX_THREED_HIDE,this.onToolboxEvent);
         this._playerProxy.addEventListener(PluginEvent.EX_CAPTION_HIDE,this.onToolboxEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.CORE_PAUSE,this.onPause);
         this._playerProxy.addEventListener(PlayerControlEvent.CORE_RESUME,this.onResume);
         this._playerProxy.addEventListener(PlayerControlEvent.CORE_TOGGLE_PAUSEPLAY,this.onPausePlay);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onSizeChange);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onSizeChange);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_READY_TO_PLAY,this.onReadyToPlay);
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_REMOVE_DEPENDECE,this._threedExplugin));
      }
      
      private function onReadyToPlay(param1:PlayerControlEvent) : void {
         var _loc2_:IThreeDData = this._playerProxy.playerData.threedInfo;
         this._threedExplugin.init(_loc2_);
      }
      
      private function onSizeChange(param1:PlayerControlEvent) : void {
         var _loc2_:Number = param1.data.width;
         var _loc3_:Number = param1.data.height;
         var _loc4_:Boolean = param1.data.fullscreen;
         var _loc5_:Number = param1.data.cheight;
         this._playerProxy.playerData.threedInfo.threedWidth = _loc2_;
         this._playerProxy.playerData.threedInfo.fullscreen = _loc4_;
         if(_loc4_)
         {
            this._playerProxy.playerData.threedInfo.threedHeight = _loc3_;
         }
         else
         {
            this._playerProxy.playerData.threedInfo.threedHeight = _loc3_ - _loc5_;
         }
      }
      
      private function onResume(param1:PlayerControlEvent) : void {
         this._isVideoPlaying = true;
         this.threedState();
      }
      
      private function onPause(param1:PlayerControlEvent) : void {
         if((param1.data) && (param1.data.hasOwnProperty("useraction")))
         {
            if(param1.data.useraction == true)
            {
               this._isVideoPlaying = false;
            }
         }
         this.threedState();
      }
      
      private function onPausePlay(param1:PlayerControlEvent) : void {
         if(this._playerProxy.playerData.videoIsPause)
         {
            this._isVideoPlaying = false;
         }
         else
         {
            this._isVideoPlaying = true;
         }
         this.threedState();
      }
      
      private function onToolboxEvent(param1:Event) : void {
         switch(param1.type)
         {
            case PlayerControlEvent.SHOW_TOOLBOX:
            case PlayerControlEvent.SHOW_EX_PLUGIN:
               this._isHidedToolbox = false;
               break;
            case PluginEvent.TOOLBOX_CLOSE:
            case PluginEvent.EX_THREED_HIDE:
            case PluginEvent.EX_CAPTION_HIDE:
               this._isHidedToolbox = true;
               break;
         }
         this.threedState();
      }
      
      private function threedState() : void {
         if(this._playerProxy.playerData.threedInfo == null)
         {
            return;
         }
         this._playerProxy.playerData.threedInfo.pause = !this._isVideoPlaying;
         if((this._isRending) && (this._isVideoPlaying) && (this._isHidedToolbox) && (this._isTVMode))
         {
            this._playerProxy.playerData.threedInfo.isTVMode = true;
         }
         else
         {
            this._playerProxy.playerData.threedInfo.isTVMode = false;
         }
      }
      
      private function onThreeDEvent(param1:ExPluginEvent) : void {
         switch(param1.type)
         {
            case "threed_render":
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_THREED_RENDER));
               this._isRending = true;
               break;
            case "report":
               this.sendActionLog(param1);
               break;
            case "hide":
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_THREED_HIDE));
               break;
            case "state_change":
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
                  {
                     "type":"threeD",
                     "state":param1.data.state
                  }));
               break;
            case "threed_mode_tv":
               this._isTVMode = true;
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_THREED_HIDE));
               break;
            case "threed_mode_other":
               this._isTVMode = false;
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_THREED_HIDE));
               break;
         }
         this.threedState();
      }
      
      private function sendActionLog(param1:ExPluginEvent) : void {
         var _loc4_:Object = null;
         var _loc2_:String = param1.data.action;
         var _loc3_:String = ReportUtil.METHOD_LEFT_CLICK;
         if(param1.data.method)
         {
            _loc3_ = param1.data.method;
         }
         if(param1.data.data)
         {
            _loc4_ = param1.data.data;
         }
         ReportUtil.sendUserActionLog(_loc2_,_loc3_,_loc4_);
      }
      
      private function onMetaDataUpdated(param1:ConductorEvent) : void {
         if(this._playerProxy.playerData.captionInfo)
         {
            this._captionExplugin.load(this.CAPTION_SWF_LOCATION);
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_ADD_DEPENDECE,this._captionExplugin));
         }
         if((this._playerProxy.playerData.threedInfo) && this._playerProxy.playerData.isRtmpPlayer == false)
         {
            this._threedExplugin.load(this.THREED_SWF_LOCATION);
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.EX_ADD_DEPENDECE,this._threedExplugin));
         }
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         switch(_loc2_)
         {
            case ConductorState.VIDEO:
               this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_TEMP_SHOW));
               this._isVideoPlaying = true;
               break;
            case ConductorState.INITIALIZE:
            case ConductorState.PRE_VIDEO:
            case ConductorState.POST_VIDEO:
            case ConductorState.AD:
               this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_TEMP_HIDE));
               this._isVideoPlaying = false;
               break;
            case ConductorState.VIDEO_END:
            case ConductorState.ERROR:
               this._playerProxy.dispatchEvent(new ExPluginControlEvent(ExPluginControlEvent.PLUGIN_HIDE_ALL));
               this._isVideoPlaying = false;
               break;
         }
         this.threedState();
      }
   }
}
